package com.br.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Font;

import javax.swing.JDesktopPane;
import java.awt.SystemColor;
import java.beans.PropertyVetoException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.swing.AbstractAction;

import com.br.controller.DocControl;
import com.br.controller.SetorControl;
import com.br.controller.UserControl;
import com.br.modelo.entidades.Setor;
import com.br.thread.AtualizaHorario;
import javax.swing.JLabel;
import java.awt.Component;
import javax.swing.Box;
import javax.swing.JComboBox;
import javax.swing.JProgressBar;
import javax.swing.JTextField;

public class TelaInicial extends JFrame{	
	private JPanel contentPane;
	private JTextField fieldUsuario;
	
	private final JLabel labelHora = new JLabel("");

	private final Splash splash = new Splash();

	private JMenu mnAministraoDoSistema = new JMenu("Aministra\u00E7\u00E3o do Sistema");

	/**
	 * Launch the application.
	 */
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TelaInicial frame = new TelaInicial();
					frame.setExtendedState(JFrame.MAXIMIZED_HORIZ);
					frame.setVisible(true);						
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	/**
	 * Create the frame.
	 */
	
	public TelaInicial() {			
		
		getContentPane().setBackground(SystemColor.scrollbar);		
		setFont(new Font("Verdana", Font.PLAIN, 12));
		setTitle("Gerenciamento de Documentos HNSG");
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 1073, 742);
		
		JMenuBar mnBarTelaInicial = new JMenuBar();
		setJMenuBar(mnBarTelaInicial);
		
		JMenu mnDocTelaInicial = new JMenu("Documentos");
		mnBarTelaInicial.add(mnDocTelaInicial);
		
		JMenu mnMovTelaIni = new JMenu("Movimenta\u00E7\u00F5es");
		mnDocTelaInicial.add(mnMovTelaIni);
				
		final JDesktopPane desktopPane = new JDesktopPane();
		desktopPane.setBackground(SystemColor.menu);
		getContentPane().add(desktopPane, BorderLayout.CENTER);
		desktopPane.setLayout(null);
		
		fieldUsuario = new JTextField();
		fieldUsuario.setBounds(37, 11, 154, 20);
		desktopPane.add(fieldUsuario);
		fieldUsuario.setEnabled(false);
		fieldUsuario.setEditable(false);
		fieldUsuario.setVisible(false);
		fieldUsuario.setColumns(10);
		addWindowListener(new WindowAdapter() {
			public void windowClosing (WindowEvent e){
				UserControl userControl = splash.userControl;
				userControl.logOff(fieldUsuario.getText(), labelHora.getText());
				System.exit(0);
			}
		});
					       		
		JMenuItem mnEntDocTelaIni = new JMenuItem("Entrada de Documento");
		mnEntDocTelaIni.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				EntradaDoc entradaDoc = splash.entradaDoc;
				entradaDoc.atualizaTela();
				
				desktopPane.removeAll();
				desktopPane.add(entradaDoc);
				entradaDoc.setVisible(true);				

				try {  
					entradaDoc.setMaximum(true);  
				} catch (PropertyVetoException ex) {  

				} 				
			}
		});
		mnMovTelaIni.add(mnEntDocTelaIni);
						
		JMenuItem mnSaidaDocTelaIni = new JMenuItem("Sa\u00EDda de Documento");
		mnSaidaDocTelaIni.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			SaidaDoc saidaDoc = splash.saidaDoc;
			saidaDoc.atualizaTela();
			
			desktopPane.removeAll();
			desktopPane.add(saidaDoc);
			saidaDoc.setVisible(true);				

			try {  
				saidaDoc.setMaximum(true);  
			} catch (PropertyVetoException ex) {  

			} 		
			}
		});
		mnMovTelaIni.add(mnSaidaDocTelaIni);
		
		JMenu mnMovimentarDocumento = new JMenu("Movimentar Documento");
		mnMovTelaIni.add(mnMovimentarDocumento);
		
		JMenuItem mntmDeSetorPara = new JMenuItem("De Setor para Setor");
		mnMovimentarDocumento.add(mntmDeSetorPara);
		
		JMenuItem mntmDeSetorPara_1 = new JMenuItem("De Setor para Arquivo");
		mnMovimentarDocumento.add(mntmDeSetorPara_1);
		
		JMenuItem mntmDeArquivoPara = new JMenuItem("De Arquivo para Setor");
		mnMovimentarDocumento.add(mntmDeArquivoPara);
		
		JMenuItem mnBaixaDocTelaIni = new JMenuItem("Baixa de Documento");
		mnMovTelaIni.add(mnBaixaDocTelaIni);
		
		JMenuItem mnPesquisarTelaIni = new JMenuItem("Perquisar ");
		mnDocTelaInicial.add(mnPesquisarTelaIni);
		
		JMenu mnArquivoTelaIni = new JMenu("Arquivo F\u00EDsico");
		mnBarTelaInicial.add(mnArquivoTelaIni);
		
		JMenu mnAlteraArqTelaIni = new JMenu("Altera\u00E7\u00F5es");
		mnArquivoTelaIni.add(mnAlteraArqTelaIni);
		
		JMenuItem mnCadArqTelaIni = new JMenuItem("Cadastrar Novo Arquivo");
		mnAlteraArqTelaIni.add(mnCadArqTelaIni);
		
		JMenuItem mnAltArqTelaIni = new JMenuItem("Alterar Arquivo Existente");
		mnAlteraArqTelaIni.add(mnAltArqTelaIni);
		
		JMenuItem mnConsArqTelaIni = new JMenuItem("Consultar");
		mnArquivoTelaIni.add(mnConsArqTelaIni);
		
		JMenu mnRelatoTelaIni = new JMenu("Relat\u00F3rios");
		mnBarTelaInicial.add(mnRelatoTelaIni);
		
		JMenu mnRelatEntSaiTelaIni = new JMenu("Relat\u00F3rio Entrada/Sa\u00EDda Documentos");
		mnRelatoTelaIni.add(mnRelatEntSaiTelaIni);
		
		JMenuItem mnRelatDataTelaIni = new JMenuItem("Por Data");
		mnRelatEntSaiTelaIni.add(mnRelatDataTelaIni);
		
		JMenuItem mnRelatSetorTelaIni = new JMenuItem("Por Setor");
		mnRelatEntSaiTelaIni.add(mnRelatSetorTelaIni);
		
		JMenu mnCadastros = new JMenu("Cadastros");
		mnBarTelaInicial.add(mnCadastros);
		
		JMenuItem mnCadSetorTelaIni = new JMenuItem("Cadastrar Setor");
		mnCadSetorTelaIni.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CadSetor cadSetor = splash.cadSetor;
				
				desktopPane.removeAll();
				desktopPane.add(cadSetor);
				cadSetor.atualizarTabela();
				cadSetor.setVisible(true);	
				
				try {  
					cadSetor.setMaximum(true);  
				} catch (PropertyVetoException ex) {  

				}	
			}
		});
		mnCadastros.add(mnCadSetorTelaIni);
		
		JMenuItem nmCadStat = new JMenuItem("Cadastrar novo Status");
		mnCadastros.add(nmCadStat);
		
		JMenuItem mnTpDoc = new JMenuItem("Cadastrar Tipo de Documento");
		mnTpDoc.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CadTpDoc cadTpDoc = splash.cadTpDoc;
				
				desktopPane.removeAll();
				desktopPane.add(cadTpDoc);
				cadTpDoc.atualizarTabela();
				cadTpDoc.setVisible(true);
				try {  
					cadTpDoc.setMaximum(true);  
				} catch (PropertyVetoException ex) {  

				}
			}
		});
		mnCadastros.add(mnTpDoc);
		
		mnBarTelaInicial.add(mnAministraoDoSistema);
		
		JMenu mnUsurios = new JMenu("Usu\u00E1rios");
		mnAministraoDoSistema.add(mnUsurios);
		
		JMenuItem mntmCriaoDeUsurio = new JMenuItem("Cria\u00E7\u00E3o de Usu\u00E1rio");
		mnUsurios.add(mntmCriaoDeUsurio);
		
		JMenuItem mntmAlteraoDeSenha = new JMenuItem("Altera\u00E7\u00E3o de Senha");
		mnUsurios.add(mntmAlteraoDeSenha);
		
		JMenu mnSair = new JMenu("Sair");
		mnBarTelaInicial.add(mnSair);
		
		Component horizontalStrut = Box.createHorizontalStrut(401);
		mnBarTelaInicial.add(horizontalStrut);
		
		
		mnBarTelaInicial.add(labelHora);

		AtualizaHorario ah = new AtualizaHorario(labelHora);
		ah.mostrarData(true);  
		Thread thHora = ah; 
		thHora.setPriority(1);
		thHora.start();
		
		JMenuItem mnSairTelaIni = new JMenuItem("Encerrar");
		mnSairTelaIni.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				UserControl userControl = splash.userControl;
				userControl.logOff(fieldUsuario.getText(), labelHora.getText());
				System.exit(0);
				
			}
		});
		mnSair.add(mnSairTelaIni);
		
		JMenuItem mntmFazerLogoff = new JMenuItem("Fazer Logoff");
		mntmFazerLogoff.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				UserControl userControl = splash.userControl;
				userControl.logOff(fieldUsuario.getText(), labelHora.getText());
				System.exit(0);
			}
		});
		mnSair.add(mntmFazerLogoff);
		
		GridBagConstraints gbc_btnTeste = new GridBagConstraints();
		gbc_btnTeste.insets = new Insets(0, 0, 5, 5);
		gbc_btnTeste.gridx = 2;
		gbc_btnTeste.gridy = 5;
				
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGap(0, 509, Short.MAX_VALUE)
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGap(0, 432, Short.MAX_VALUE)
		);
		contentPane.setLayout(gl_contentPane);
	}
	private class SwingAction extends AbstractAction {
		public SwingAction() {
			putValue(NAME, "SwingAction");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
		}
	}
	
	public void setCorrentUser(String correntUser){
		fieldUsuario.setText(correntUser);
	}
	
	public String returnData() {
		String data = labelHora.getText();
		return data;
	}
	
	public void verificaAdmin(String user) {
		String admin = splash.userControl.verificaAdmin(user);
		if(admin == null){
			mnAministraoDoSistema.setEnabled(false);
		}
		if(admin == "s"){
			mnAministraoDoSistema.setEnabled(true);
		}
	}
}
